package com.lry.rpc.starter.extend;

import com.lry.rpc.netty.server.RpcServer;
import com.lry.rpc.starter.annotation.RpcService;
import com.lry.rpc.starter.bean.ServiceBean;
import com.lry.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

@Slf4j
public class ServiceAnnotationBeanPostProcessor implements BeanDefinitionRegistryPostProcessor  {

    public final static String LOG_PREFIX = ">>>>>>>>ServiceAnnotationBeanPostProcessor-";

    private Set<Class<?>> classSet;

    public ServiceAnnotationBeanPostProcessor(Set<Class<?>> classSet){
        this.classSet = classSet;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        if(CollectionUtils.isEmpty(classSet)){
            log.warn("classSet is empty , ServiceBean registry will be ignored!");
            return;
        }

        for (Class<?> clazz : classSet) {
            if(clazz.getInterfaces().length==0){
                log.warn("clazz={} not impl any interface,so we will ignore it",clazz.getName());
                continue;
            }
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ServiceBean.class);
            AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
            builder.addConstructorArgValue(clazz);
            builder.setRole(2);
            BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, registry);
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }



}
